// pages/fromIn/fromIn.js
Page({

  /**
   * 页面的初始数据
   */
  data: {
    day:'',
    month:'',
    routes: [
      {
        stops: [
          { name: "太平地铁站A出口", time: "08:20", selected: false },
          { name: "新和地铁站C出口", time: "08:50", selected: false },
          { name: "钟落潭高校园公交站", time: "09:30", selected: false }
        ]
      },
      {
        stops: [
          { name: "宝安中心地铁站", time: "09:00", selected: false },
          { name: "深大北门", time: "09:30", selected: false },
          { name: "世界之窗", time: "10:00", selected: false }
        ]
      }
    ],
    selectedOrigin: null,
    selectedTerminus: null
  },

  selectStop(e) {
    // 获取点击的站点类型和索引
    const type = e.currentTarget.dataset.type;
    const index = e.currentTarget.dataset.index;
    
    // 获取当前点击的站点
    const currentRouteIndex = type === 'origin' ? 0 : 1;
    const currentStop = this.data.routes[currentRouteIndex].stops[index];
    
    // 如果当前站点已经是选中状态，则直接返回（防止重复点击变色）
    if (currentStop.selected) {
      return;
    }
    
    
    // 生成数据路径
    const path = `routes[${currentRouteIndex}].stops[${index}].selected`;
    
    // 重置所有选中状态
    this.data.routes.forEach(route => {
      route.stops.forEach(stop => {
        stop.selected = false;
      });
    });
    
    // 设置当前选中状态
    this.setData({
      [path]: true,
      [`selected${type === 'origin' ? 'Origin' : 'Terminus'}`]: index
    });
  },
  
  settle() {
    // 检查是否已选择上下车站点
    if (this.data.selectedOrigin === null || this.data.selectedTerminus === null) {
      wx.showToast({
        title: '请选择上下车站点',
        icon: 'none'
      });
      return;
    }
  
    // 获取选中的上下车站点信息
    const origin = this.data.routes[0].stops[this.data.selectedOrigin];
    const terminus = this.data.routes[1].stops[this.data.selectedTerminus];
    const { day, month} = this.data;
  
    // 传递数据到下一页
    wx.navigateTo({
      url: `/pages/settle/settle?originName=${origin.name}&originTime=${origin.time}&terminusName=${terminus.name}&terminusTime=${terminus.time}&day=${day}&month=${month}`
    });
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad(options) {
    console.log(options)
    this.setData({
      day: options.day,
      month: options.month
    })
  },
})